package cn.icanci.loopstack.ddk.admin.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import cn.hutool.json.JSONUtil;
import cn.icanci.loopstack.ddk.admin.biz.event.log.LogEvent;
import cn.icanci.loopstack.ddk.admin.biz.mapping.config.AppConfigMapping;
import cn.icanci.loopstack.ddk.admin.biz.service.AppConfigService;
import cn.icanci.loopstack.ddk.admin.biz.service.BaseService;
import cn.icanci.loopstack.ddk.admin.biz.utils.EnvUtils;
import cn.icanci.loopstack.ddk.admin.dal.mapper.entity.AppConfigDO;
import cn.icanci.loopstack.ddk.admin.dal.mapper.mapper.AppConfigMapper;
import cn.icanci.loopstack.ddk.common.enums.LogOperatorTypeEnum;
import cn.icanci.loopstack.ddk.common.enums.ModuleTypeEnum;
import cn.icanci.loopstack.ddk.common.model.PageList;
import cn.icanci.loopstack.ddk.common.model.TextValue;
import cn.icanci.loopstack.ddk.common.model.config.AppConfigVO;
import cn.icanci.loopstack.ddk.common.utils.PaginatorUtils;

/**
 * @author icanci
 * @since 1.0 Created in 2023/01/06 09:45
 */
@Service
public class AppConfigServiceImpl extends BaseService<AppConfigVO> implements AppConfigService {
    @Resource
    private AppConfigMapper  appConfigMapper;
    @Resource
    private AppConfigMapping appConfigMapping;

    @Override
    public PageList<AppConfigVO> queryPage(AppConfigVO vo, int currentPage, int pageSize) {
        vo.setEnv(getEnv());
        vo.setIsDelete(null);

        currentPage = Math.max(currentPage - 1, 0);

        AppConfigDO ddkAppConfig = appConfigMapping.vo2do(vo);
        List<AppConfigDO> ret = appConfigMapper.selectPage(ddkAppConfig, currentPage, pageSize);

        return PaginatorUtils.formatPaginator(appConfigMapping.dos2vos(ret), currentPage, pageSize, appConfigMapper.total(ddkAppConfig));
    }

    @Override
    public void save(AppConfigVO vo) {
        if (doInsert(vo)) {
            beforeInsert(vo);
            AppConfigDO appConfigDO = appConfigMapping.vo2do(vo);
            appConfigMapper.insert(appConfigDO);
            eventDispatcher.fire(new LogEvent(vo.getUuid(), ModuleTypeEnum.DDK_APP_CONFIG, JSONUtil.toJsonStr(appConfigDO), LogOperatorTypeEnum.CREATE), false);
        } else {
            beforeUpdate(vo);
            AppConfigDO appConfigDO = appConfigMapping.vo2do(vo);
            appConfigMapper.update(appConfigDO);
            eventDispatcher.fire(new LogEvent(vo.getUuid(), ModuleTypeEnum.DDK_APP_CONFIG, JSONUtil.toJsonStr(appConfigDO), LogOperatorTypeEnum.UPDATE), false);
        }
    }

    @Override
    public AppConfigVO queryByAppConfigName(String appConfigName) {
        AppConfigDO config = appConfigMapper.queryByAppConfigName(appConfigName, EnvUtils.getEnv());
        return appConfigMapping.do2vo(config);
    }

    @Override
    public List<TextValue> loadSelector() {
        AppConfigDO appConfigDO = new AppConfigDO();
        appConfigDO.setEnv(getEnv());

        List<AppConfigDO> apps = appConfigMapper.selectList(appConfigDO);

        if (CollectionUtils.isEmpty(apps)) {
            return Lists.newArrayList();
        }

        List<AppConfigVO> appConfigVOS = appConfigMapping.dos2vos(apps);
        List<TextValue> textValues = Lists.newArrayList();
        for (AppConfigVO appConfigVO : appConfigVOS) {
            String label;
            if (isDeleted(appConfigVO)) {
                label = String.format(DELETED_FORMAT, appConfigVO.getAppConfigName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, appConfigVO.getAppConfigName());
            }
            String value = appConfigVO.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    @Override
    public List<AppConfigVO> queryByAppUuid(String appUuid, Integer isDelete) {
        AppConfigDO appConfigDO = new AppConfigDO();
        appConfigDO.setEnv(getEnv());
        appConfigDO.setAppUuid(appUuid);
        appConfigDO.setIsDelete(isDelete);
        List<AppConfigDO> apps = appConfigMapper.selectList(appConfigDO);
        return appConfigMapping.dos2vos(apps);
    }

    @Override
    public AppConfigVO queryByUuid(String uuid) {
        AppConfigDO app = appConfigMapper.queryByUuid(getEnv(), uuid);
        return appConfigMapping.do2vo(app);
    }
}
